package com.burgeon.backend.college.control_panel.service.util;

import com.burgeon.backend.college.control_panel.entity.ExamItem;
import com.burgeon.backend.college.control_panel.entity.Option;
import com.burgeon.backend.college.control_panel.mappers.BackendCollegeMapper;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class ReadUtil {

    @Autowired
    private BackendCollegeMapper backendCollegeMapper;

    public List<ExamItem> ReadXls(String id, MultipartFile sourceFile) throws IOException {
        InputStream inputStream = null;
        File file = null;

        file = File.createTempFile("temp", null);
        sourceFile.transferTo(file);   //sourceFile为传入的MultipartFile
        inputStream = new FileInputStream(file);
        file.deleteOnExit();
        List<ExamItem> examItems = new ArrayList<ExamItem>();
        HSSFWorkbook book = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = book.getSheetAt(0);


        /**
         * 第一行都是标题，所以从第二行开始读取数据
         */
        HashMap<String, String> resultMap = new HashMap<String, String>();
        for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
            HSSFRow row = sheet.getRow(i);
            if (row == null) {
                System.err.println("123123123123123123");
                continue;

            }

            try {
                String oA = null, oB = null, oC = null, oD = null;
                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                String title = row.getCell(0).getStringCellValue(); //名称

                if (row.getCell(1) != null) {
                    row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                    oA = row.getCell(1).getStringCellValue();
                }

                if (row.getCell(2) != null) {
                    row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                    oB = row.getCell(2).getStringCellValue();
                }

                if (row.getCell(3) != null) {
                    row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
                    oC = row.getCell(3).getStringCellValue();
                }

                if (row.getCell(4) != null) {
                    row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
                    oD = row.getCell(4).getStringCellValue();
                }

                row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);


                String result = row.getCell(5).getStringCellValue();
                String score = row.getCell(6).getStringCellValue();


                ExamItem examItem = new ExamItem();
                examItem.setExamId(id);
                examItem.setResult(result);
                examItem.setTitle(title);
                examItem.setScore(Integer.parseInt(score));
                examItem.setOptions(new ArrayList<Option>());
                examItem.setNum(i);

                backendCollegeMapper.createExamItem(examItem);

                String aid = createOption(oA, examItem);
                resultMap.put("A", aid);
                String bid = createOption(oB, examItem);
                resultMap.put("B", bid);
                String cid = createOption(oC, examItem);
                resultMap.put("C", cid);
                String did = createOption(oD, examItem);
                resultMap.put("D", did);

                backendCollegeMapper.upExamItem(examItem.getId(), title, Integer.parseInt(score), resultMap.get(result));

                examItems.add(examItem);
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }


        for (ExamItem examItem : examItems) {
            System.err.println(examItem);
        }

        return examItems;

    }

    private String createOption(String value, ExamItem examItem) {
        if (null != value) {
            Option option = new Option();
            option.setExamItemId(examItem.getId());
            option.setValue(value);
            backendCollegeMapper.createOption(option);
            examItem.getOptions().add(option);

            return option.getId();

        }
        return null;
    }
}
